//===================================================================================
// Microsoft patterns & practices
// Composite Application Guidance for Windows Presentation Foundation and Silverlight
//===================================================================================
// Copyright (c) Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===================================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===================================================================================
using System.Windows;
using Microsoft.Practices.Prism.Modularity;
using Microsoft.Practices.Prism.UnityExtensions;
using Microsoft.Practices.Unity;
using DXTools.UI.Shell;
using System;
using System.IO;

namespace DXTools.UI.Shell
{
    public class Bootstrapper : UnityBootstrapper
    {
        private Stream _moduleCatalog;

        public Bootstrapper(Stream moduleCatalog)
        {
            this._moduleCatalog = moduleCatalog;
        }

        protected override DependencyObject CreateShell()
        {
            return Container.Resolve<MainShellView>();
        }

        protected override void InitializeShell()
        {
            base.InitializeShell();
            Application.Current.RootVisual = (UIElement)this.Shell;
        }

        protected override IModuleCatalog CreateModuleCatalog()
        {
            try{
                base.ConfigureModuleCatalog();
                return Microsoft.Practices.Prism.Modularity.ModuleCatalog.CreateFromXaml(this._moduleCatalog);
            }
            catch (Exception ex)
            {
                throw new Exception("Error occurred while configuring module catalog, check configuration xml is valid. " + ex.Message);
            }
            //Other options:

            //Programatically
            //ModuleCatalog m = new ModuleCatalog();
            //ModuleInfo m1 = new ModuleInfo();
            //m1.InitializationMode = InitializationMode.WhenAvailable;
            //m1.ModuleName = "DXTools.UI.Modules.Sample";
            //m1.ModuleType = "DXTools.UI.Modules.Sample.ModuleInit, DXTools.UI.Modules.Sample, Version=1.0.0.0";
            //m1.Ref = "DXTools.UI.Modules.Sample.xap";
            //m.AddModule(m1);
            //return m;
            
            //Using URL to retriveModuleCatalog.xaml
            //return Microsoft.Practices.Prism.Modularity.ModuleCatalog.CreateFromXaml(
            //    new Uri(ModuleCatalogUri, UriKind.Relative));            
        }
    }
}
